<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%
  	String path = request.getContextPath();
	String basePath = "/";
%>
<!DOCTYPE html>
<html lang="en">
<head>
<base href="/">
<!-- 下拉框 -->
<link rel="stylesheet" href="static/ace/css/chosen.css" />

<link href="plugins/kindeditor/themes/simple/simple.css" type="text/css" rel="stylesheet">
<link rel="stylesheet" href="static/ace/css/datepicker.css" />
<link href="//cdn.bootcss.com/ionicons/2.0.1/css/ionicons.min.css" rel="stylesheet">
<link href="static/styles/base.css" rel="stylesheet">
<!-- jsp文件头和头部 -->
<%@ include file="../index/top.jsp"%>
<style>
.disabled{color:#000;cursor: default;}
.disabled:hover{color:#000;text-decoration:none;}
.update-img{width:80px;height:80px;display:flex;align-items: center;justify-content: center;overflow: hidden;border: 1px solid #ccc;}
.update-img img{
max-width:100%;max-height:100%;
}
</style>
</head>
<body class="no-skin">
  <!-- /section:basics/navbar.layout -->
  <div class="main-container" id="main-container">
    <!-- /section:basics/sidebar -->
    <div class="main-content">
      <div class="main-content-inner">
        <div class="page-content">
          <div class="row">
            <div class="col-xs-12">
              <form action="retrospect/updateSHState.do" name="goodsForm" id="goodsForm" method="post">
               <input type="hidden" id="state" value="${farm.sh_state }" />
               <input type="hidden" id="id" name="id" value="${farm.id }" />
                <div id="zhongxin" style="padding-top: 13px;">
                  <table id="table_report"
                    class="table table-striped table-bordered table-hover">
                    <tr>
                      <td style="width: 100px; text-align: right; padding-top: 13px;"> 基地:&nbsp;</td>
                      <td><input type="text" value="${farm.base_name }" maxlength="100" style="width: 98%;" readonly="readonly"/></td>
                      <td style="width: 100px; text-align: right; padding-top: 13px;">维护员:&nbsp;</td>
                      <td><input type="text" value="${farm.name }" maxlength="100" style="width: 98%;" readonly="readonly"/></td>
                       <td style="width: 100px; text-align: right; padding-top: 13px;">维护员ID:&nbsp;</td>
                      <td><input type="text" value="${farm.operator_id }" maxlength="100" style="width: 98%;" readonly="readonly"/></td>
                    </tr>
                     <tr>
                      <td style="width: 100px; text-align: right; padding-top: 13px;"> 农事类型:&nbsp;</td>
                      <td><input type="text" value="${farm.type }" maxlength="100" style="width: 98%;" readonly="readonly"/></td>
                      <td style="width: 100px; text-align: right; padding-top: 13px;">维护区域:&nbsp;</td>
                      <td><input type="text" value="${farm.operation_area }" maxlength="100" style="width: 98%;" readonly="readonly"/></td>
                       <td style="width: 100px; text-align: right; padding-top: 13px;">维护时间:&nbsp;</td>
                      <td><input type="text" value="${farm.operation_time }" maxlength="100" style="width: 98%;" readonly="readonly"/></td>
                    </tr>
                     <tr>
                      <td style="width: 79px; text-align: right; padding-top: 13px;">图片:&nbsp;</td>
                      <td colspan="5"><a href="javascript:void(0)" class="picFileUpload"></a> 
                      <input type="hidden" id="picture" name="picture" id="picture" value="${farm.picture_video }" /></td>
                    </tr>
                     <tr>
                     <td style="width: 100px; text-align: right; padding-top: 13px;">备注:&nbsp;</td>
                     <td colspan="5"><input type="text" value="${farm.remark }" maxlength="100" style="width: 98%;" readonly="readonly"/></td>
                    </tr>
                    <tr>
                     <td style="width: 100px; text-align: right; padding-top: 13px;">描述:&nbsp;</td>
                     <c:if test="${farm.desc_info == null }">
                     	<td colspan="5"><input type="text" id="desc_tips" name="desc_tips" value="${farm.desc_tips }" maxlength="14" style="width: 98%;"/></td>
                     </c:if>
                     <c:if test="${farm.desc_info != null }">
                     	<td colspan="5"><input type="text" id="desc_tips" name="desc_tips" value="${farm.desc_info }" maxlength="14" style="width: 98%;"/></td>
                     </c:if>
                    </tr>
 					<tr>
 					<td style="width: 100px; text-align: right; padding-top: 13px;">审核状态:&nbsp;</td>
 					<td colspan="6">
	                  &emsp; <input id="yes" name="sh_state" type="radio" value="2" />&nbsp;通过&emsp;&emsp;&emsp;
					   <input id="no" name="sh_state" type="radio" value="3" />&nbsp;拒绝 &emsp;
					   不通过原因: &nbsp;<input type="text" id="sh_desc" name="sh_desc" value="${farm.sh_desc }" maxlength="10" style="width: 70%;"/>
					</td>
                    </tr>
                    
                  </table>
                   <table id="table_report1"
                    class="table table-striped table-bordered table-hover">
                       <tr>
                        <td style="text-align: center;" colspan="10">
                        <c:if test="${farm.sh_state !=2 && QX.add == 1 }">
                          <a class="btn btn-mini btn-primary"
                          onclick="save();">提交</a>
                          <a class="btn btn-mini btn-danger"
                          onclick="top.Dialog.close();">取消</a>
                        </c:if>
                        </td>
                      </tr>
                    </table>
                </div>
                <div id="zhongxin2" class="center" style="display: none">
                  <br /> <br /> <br /> <br /> <img
                    src="static/images/jiazai.gif" /><br />
                  <h4 class="lighter block green"></h4>
                </div>
              </form>
            </div>
            <!-- /.col -->
          </div>
          <!-- /.row -->
        </div>
        <!-- /.page-content -->
      </div>
    </div>
    <!-- /.main-content -->
  </div>


  <!-- /.main-container -->
  <!-- basic scripts -->
  <!-- 页面底部js¨ -->
  <%@ include file="../index/foot.jsp"%>
  <!-- ace scripts -->
  <script src="static/ace/js/ace/ace.js"></script>
  <!-- inline scripts related to this page -->
  <script src="static/ace/js/bootbox.js"></script>
  <!-- 下拉框 -->
  <script src="static/ace/js/chosen.jquery.js"></script>
  <!--提示框-->
  <script type="text/javascript" src="static/js/jquery.tips.js"></script>
  <script type="text/javascript" src="static/js/common/file_upload1.js"></script>
  <script type="text/javascript" src="plugins/kindeditor/kindeditor-all.js"></script>
  <script type="text/javascript" src="plugins/kindeditor/lang/zh_CN.js"></script>
  <script type="text/javascript" src="static/laydate/laydate.js"></script>
  <script type="text/javascript" src="static/js/provincial.municipal.league.js"></script>
</body>
<script type="text/javascript">
  $(top.hangge());
  var itemAddEditor ;
  var currentIndex = 0;
  //页面初始化完毕后执行此方法
  var min_ratio=$("#min_ratio").val();
  var max_ratio=$("#max_ratio").val();
  
  
  $(function(){
   var state= $("#state").val();
   if(state ==2){
	   $("#yes").attr("checked", true);
   }else if(state ==3){
	   $("#no").attr("checked", true);
   }
   
   
    $("#isPack").on("change",function(){
      var value = $(this).val();
      if($.trim(value) == '00'){
        $("#transitPrice").attr("readonly",false);
      }else{
        $("#transitPrice").attr("readonly",true);
        $("#transitPrice").val("0.00")
      }
    })
    
    SpecificationsBtn();
    //创建富文本编辑器
  <%--   TAOTAO.kingEditorParams = {
      //指定上传文件参数名称
      filePostName  : "uploadFile",
      //指定上传文件请求的url。
      uploadJson : '<%=basePath%>pic/upload',
      //上传类型，分别为image、flash、media、file
      dir : "image",
      extraFileUploadParams:{isCom:"01",width:800,height:800,quality:0.8,end:"view"}
    } --%>
    
    TAOTAO.kingEditorAutoParams = {
        //指定上传文件参数名称
        filePostName  : "uploadFile",
        //指定上传文件请求的url。
        uploadJson : '<%=basePath%>pic/farmWork',
        //上传类型，分别为image、flash、media、file
        dir : "image",
        extraFileUploadParams:{isCom:"01",width:200,height:200,quality:1,end:"thumb"}
      }
    
    goodsAddEditor = TAOTAO.createEditor("#goodsForm [name=content]");
    //初始化类目选择和图片上传器
    TAOTAO.initPicUpload($("input[name=picture]").val());
    TAOTAO.initOnePicUpload($("input[name=thumb]").val());
    
    
    $('body').on('click','.imageButton',function(){
      var state=$("#state").val();
	      if(state == 2){
	    	  return false;
	      }
 		  var id=$("#id").val();
 	      var picture=$("#picture").val();
 	      var imgsrc=$(this).parent('li').find("img").attr("src");
 	      var _this = $(this);
 	      var others = $(this).parent('li').siblings()
 	      var newPicture="";
 	      if(picture.indexOf((imgsrc+',')) >-1){
 	        newPicture=picture.replace((imgsrc+','),'');
 	      }else if(picture.indexOf((','+imgsrc+'')) > -1){
 	        newPicture=picture.replace((','+imgsrc+''),'');
 	      }
 	      bootbox.confirm("确定删除图片？", function(result) {
 	    	  if(result){
    	      $.ajax({
    	        url:"<%=basePath%>pic/farmWordDelete",
    	        type:"post",
    	        async:true,
    	        dataType:"text",
    	        data:{"id":id,"newPicture":newPicture,"imgsrc":imgsrc,"state":"00"},
    	        success : function(data) {
    	          if(data == "success"){
    	            _this.parent('li').remove();
    	            var html =''
    	            for(var i=0;i<others.length;i++){
    	            	var $item = $(others[i])
    	            	var val = $item.find('img').attr('src')
    	            	html+=val+','
    	            }
    	            console.log(html)
    	            
    	            $("#picture").val(html); 
    	          }
    	        },error : function(XMLHttpRequest, textStatus){
    	          alealert(textStatus);
    	        }
    	      }); 
    	  }
      });
        
    });
  });
  $("#costPrice").change(function(){
    $("#agentPrice").val(($(this).val()*1.1).toFixed(2));
  });
  $("#price").change(function(){
    if($("#isSales").val()=='00'||'${msg}'=='saveG'){
      $("#realPrice").val($(this).val());
    }
  });
  
  $("#isSales").change(function(){
    var tempPrice=$("#tempPrice").val();
    if($(this).val()=='00'&&'${msg}'=='editG'){
      $("#realPrice").val($("#price").val());
    }else if($(this).val()=='01'&&'${msg}'=='editG'){
      $("#realPrice").val(tempPrice);
    }
    
  });
  
  function countDiscount(){
    if($.trim($("#marketPrice").val())==""){
      alert("市场价为空无法计算");
      return false;
    }
    if($.trim($("#bidPrice").val())==""){
      alert("进货价价为空无法计算");
      return false;
    } 
    if($("#marketPrice").val()==0){
      alert("市场价不能等于0");
      return false;
    }
    if($("#bidPrice").val()==0){
      alert("进货价不能等于0");
      return false;
    }
    var discount=(toDecimal($("#bidPrice").val())/($("#marketPrice").val())*10).toFixed(2);
    if(Number(discount)<=0||Number(discount)>=10){
      alert("折扣比例不正确，请检查进货价和市场价");
      $("#discount").val("");
      return false;
    }
    $("#discount").val(discount);
    if(Number(discount)>max_ratio){
      $("#isPointsBuy").val("00");
      $("#changePoints").val(0);
      $.ajax({  
         type:'post',  
         url:'goods/getReturnPoints.do',
         async:false, 
         data:{"discount":discount},   
         success:function(data){ 
           if("failed"==data){
             alert("没有找到对应返还比例");
             return false;
           }
           if($("#isSales").val()=='00'){$("#changePrice").val($("#price").val()); $("#returnPoints").val(parseInt($("#price").val()*Number(data)/100));}
           else{$("#changePrice").val($("#realPrice").val());$("#returnPoints").val(parseInt($("#realPrice").val()*Number(data)/100));}
           }
         });
    }else if(min_ratio<Number(discount)&&Number(discount)<=max_ratio){
      $("#isPointsBuy").val("01");
      $("#returnPoints").val(0);
      if($("#isSales").val()=='00'){
      $("#changePoints").val(parseInt($("#price").val()*(1-Number(discount)/10)/(1-min_ratio/10)));
      $("#changePrice").val(Number($("#price").val())-Number($("#changePoints").val()));
      }else{
      $("#changePoints").val(parseInt($("#realPrice").val()*(1-Number(discount)/10)/(1-min_ratio/10)));
      $("#changePrice").val(Number($("#realPrice").val())-Number($("#changePoints").val()));  
      }
    }else{
      $("#isPointsBuy").val("01");
      $("#changePrice").val(0);
      $("#returnPoints").val(0);
      if($("#isSales").val()=='00'){$("#changePoints").val(Math.ceil($("#price").val()));}
      else{$("#changePoints").val(Math.ceil($("#realPrice").val()));}
    }
  }
  function save(){
	  
        if($.trim($("#desc_tips").val())==""){
            alert("请输入农事描述");
            return false;
        }
        if(document.getElementById("yes").checked){
        	bootbox.confirm("确定通过本条农事的审核?", function(result) {
        		if(result) {
        	
        		      $("#goodsForm").submit(); 
        		      $("#zhongxin").hide();
        		      $("#zhongxin2").show(); 
        		};
        	});
        } else if(document.getElementById("no").checked){
        		bootbox.confirm("确定不通过审核？", function(result) {
            		if(result) {
            			if($.trim($("#sh_desc").val())==""){
            				alert("请输入不通过原因！");
            				return false;
            			} else {
            			      $("#goodsForm").submit(); 
            			      $("#zhongxin").hide();
            			      $("#zhongxin2").show(); 
            			}
            		};
            	});
        	
        } else {
        	  alert("请选择审核状态");
              return false;
        }
  }
  $(".chooseClass").on("click",function(){
     var diag = new top.Dialog();
     diag.Drag=true;
     diag.Title ="选择商品分类";
     diag.URL = '<%=basePath%>classify/toClassifyPage.do';
    diag.Width = 500;
    diag.Height = 500;
    diag.CancelEvent = function() {
      diag.close();
    }, diag.OKEvent = function() {
      var child = diag.innerFrame.contentWindow.document;
     var classId= $(child).find("#classId").val()
     $.ajax({
           url:"<%=basePath%>classify/getClassifyIsP",
           type:"post",
           async:true,
           dataType:"text",
           data:{"classId":classId},
           success : function(data) {
              if("success" ==data){
            	alert("请选择二级分类!");
              }else{
            	  $("#classId").val(classId);
                  $("#className").html($(child).find("#className").val());
              }
           }
         });
      diag.close();
    };//点击确定后调用的方法
    diag.show();
  })
  
  
  function getInputVal($parent){
 		  var $table = $parent.find('.js-goods-standard');
 		  var ths = $table.find('thead th')
 		  var trs = $table.find('tbody tr')
 		  var html=''
 		  var data=[{
 			 key:[],
 			 value:[]
 		  }]
 		  for(var i=1;i<ths.length;i++){
 			  var val = ths.eq(i).text()
 			  data[0].key.push(val)
 		  } 		  
    	  for(var i=0;i<trs.length;i++){
    		  var tds = trs.eq(i).find('td')
    		  var text ='';
    		  data[0].value.push([])
    		  for(var j=1;j<tds.length;j++){
    			  var val =tds.eq(j).find('input[name=stock]').val()
    			  var index = tds.eq(j).attr('data-index')
    			  if(index){
    				  if(j === tds.length-2){
    					  text +=index
    				  }else{
    					  text +=index+'-'
    				  }
    			  }
    			  if(!!val){
    				  data[0].value[i].push($.trim(val));
    			  }
    			 
    		  }
    		  data[0].value[i].push(text);
    	  }
    	  var data=JSON.stringify(data);
    	 //$("#param").val(data);
    	 
    	 return data;
 	  }
  
  
   $("#selectSpec").on("click",function(){
	   if($(this).hasClass('disabled')){
		   return false;
	   }
	 var $this = $(this);
     var diag = new top.Dialog();
     diag.Drag=true;
     diag.Title ="添加规格";
     diag.URL = '<%=basePath%>goods/toGoodsSpec.do';
     diag.Width = 1200;
     diag.Height = 500;
     diag.CancelEvent = function() {
     diag.close();
    }, diag.OKEvent = function() {
      var child = diag.innerFrame.contentWindow.document;
      //[{"key":["规格1","库存"],"value":[["1","p1"]]}]
      
      var param = getInputVal($(child))
      
      var attr=$(child).find("#attr").val();
      //var param=$(child).find("#param").val();
      
      console.log(param)
      
      if(attr !="" &&param != ""){
        $("#attr").val(attr);
        $("#param").val(param)
        $("#gS").html("已添加")
        $this.addClass('disabled')
      }
      diag.close();
    };//点击确定后调用的方法
    diag.show();
  })
  
  
  
  
  $("#selectUnitSpan").on("click",function(){
	  	 var $this = $(this);
	     var isSelectUnit = $this.find("input").val();
	     if(isSelectUnit == '00'){
	    	 $this.find("input").prop("checked",true); 
	      }else{
	    	 $this.find("input").prop("checked",false); 
	      }
	     var diag = new top.Dialog();
	     diag.Drag=true;
	     diag.Title ="多单位设置";
	     diag.URL = '<%=basePath%>goods/unit.do';
	     diag.Width = 500;
	     diag.Height = 500;
	     diag.CancelEvent = function() {
	      diag.close();
	      if(isSelectUnit == '00'){
	    	  $this.find("input").prop("checked",false); 
	      }else{
	    	  $this.find("input").prop("checked",true); 
	      }
	    }, diag.OKEvent = function() {
	      var child = diag.innerFrame.contentWindow.document;
	      diag.close();
	    };//点击确定后调用的方法
	    diag.show(); 
	  
  })
  
  //decimal类型判断
  function isDecimal(item) {
    var obj = $(item);
    if (obj.length > 0) {
      if ($(obj).val() != null && typeof ($(obj).val()) != "undefined") {
        var str = $(obj).val().toString();
        if (str != "") {
          var pattern = '^-?[1-9]\\d*$|^-?0\\.\\d*$|^-?[1-9]\\d*\\.\\d*$';
          var reg = new RegExp(pattern, 'g');
          if (reg.test(str)) {
            return true;
          } else {
            if (str.match(/[^0-9\.-]/g) != null) {
              if (str.match(/[^0-9\.-]/g).length > 0) {
                $(item).val('');
              }
            }
          }
        }
      }
    }
    return false;
  }
  //检查是否为正整数
  function isInt(a) {
    var reg = /^\d+$/;
    return reg.test(a);
  }
  var SpecificationsBtn = function() {
    $(".SpecificationsBtn").on("click", function() {

    })
  }
  //检查输入对象的值是否符合整数格式
  function isInteger(str) {
    var regu = /^[-]{0,1}[0-9]{1,}$/;
    return regu.test(str);
  }

  var removeImg = function() {

  }

  function toDecimal(x) {
    var f = parseFloat(x);
    if (isNaN(f)) {
      return;
    }
    f = Math.round(x * 100) / 100;
    return f;
  }

  function addTr(tr) {
    var type = $("#isAdvanceState").val();
    if (type == '01') {
      $('.advanceEle').show()
      $('.payment').show()
    } else {
      $('.advanceEle').hide()
      $('.payment').hide()
    }
  }

</script>
</html>